package com.luck.easyfoodbatch.adapter;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.MyOrdersM;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyfoodbatch.util.XLHRatingBar;
import com.luck.easyrecyclerview.adapter.BaseViewHolder;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.picture.lib.model.FunctionConfig;
import com.luck.picture.lib.model.PictureConfig;
import com.yalantis.ucrop.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class CommentEvaluateAdapter extends RecyclerArrayAdapter<MyOrdersM.DataBean.ModelsBean.OrderInfoBean> {
    private Context mContext;
    protected static final int REQUEST_STORAGE_READ_ACCESS_PERMISSION = 101;
    private List<LocalMedia> photoInfos = new ArrayList<>();

    public CommentEvaluateAdapter(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

        return new CouponViewHolder(parent);
    }

    public class CouponViewHolder extends BaseViewHolder<MyOrdersM.DataBean.ModelsBean.OrderInfoBean> {
        GridImageAdapter2 adapter;
        XLHRatingBar ratingBar3;
        EditText et_evaluate;
        ImageView iv_banner;
        RecyclerView rv_select_photo;
        MyOrdersM.DataBean.ModelsBean.OrderInfoBean mBean;

        public CouponViewHolder(ViewGroup parent) {
            super(parent, R.layout.comment_evaluate);
            ratingBar3 = $(R.id.ratingBar3);
            et_evaluate = $(R.id.et_evaluate);
            iv_banner = $(R.id.iv_banner);
            rv_select_photo = $(R.id.rv_select_photo);
        }

        @Override
        public void setData(final MyOrdersM.DataBean.ModelsBean.OrderInfoBean bean, int position) {
            this.mBean = bean;
            String thumb = bean.getGoods_thumb();
            if (!Tools.isNull(thumb)) {
                String path = "";
                if (thumb.startsWith("http")) {
                    path = thumb;
                } else {
                    path = HttpUtil.IMG_THUMB_BASE_URL + thumb;
                }
                Glide.with(mContext)
                        .load(path)
                        .placeholder(R.mipmap.loading_4b3)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(iv_banner);
            } else {
                iv_banner.setImageResource(R.mipmap.loading_4b3);
            }

            bean.setG_star(ratingBar3.getCountSelected() + "");
            et_evaluate.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    bean.setContent(editable.toString());
                }
            });
            adapter = new GridImageAdapter2(mContext, onAddPicClickListener);
            LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
            mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rv_select_photo.setLayoutManager(mLayoutManager);
            rv_select_photo.setAdapter(adapter);
            adapter.setOnItemClickListener(new GridImageAdapter2.OnItemClickListener() {
                @Override
                public void onItemClick(int position, View v) {

                }
            });
            ratingBar3.setOnRatingChangeListener(new XLHRatingBar.OnRatingChangeListener() {
                @Override
                public void onChange(int countSelected) {
                    bean.setG_star(countSelected + "");
                }
            });

        }

        PictureConfig.OnSelectResultCallback resultCallback = new PictureConfig.OnSelectResultCallback() {
            @Override
            public void onSelectSuccess(List<LocalMedia> list) {
                photoInfos.addAll(list);
                adapter.setPhotoInfos(photoInfos);
                adapter.notifyDataSetChanged();
                mBean.setPhotoInfos(photoInfos);
            }
        };
        private GridImageAdapter2.onAddPicClickListener onAddPicClickListener = new GridImageAdapter2.onAddPicClickListener() {
            @Override
            public void onAddPicClick(int type, int position) {

                switch (type) {
                    case 0:
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN // Permission was added in API Level 16
                                && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE)
                                != PackageManager.PERMISSION_GRANTED) {
                            requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE,
                                    mContext.getString(R.string.mis_permission_rationale),
                                    REQUEST_STORAGE_READ_ACCESS_PERMISSION);
                        } else {
                            int size = 5 - photoInfos.size();
                            if (size == 0) {
                                size = 5;
                            }
                            FunctionConfig options = new FunctionConfig();
                            options.setCompress(true);
                            options.setMaxSelectNum(size);
                            PictureConfig.init(options);
                            PictureConfig.getPictureConfig().openPhoto(mContext, resultCallback);
                        }
                        break;
                    case 1:
                        if (photoInfos.size() > 0) {
                            photoInfos.remove(position);
                        }
                        adapter.notifyItemRemoved(position);
                        adapter.notifyDataSetChanged();
                        break;
                }
            }
        };

        private void requestPermission(final String permission, String rationale, final int requestCode) {
            if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) mContext, permission)) {
                new AlertDialog.Builder(mContext)
                        .setTitle("获取相册权限")
                        .setMessage(rationale)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ActivityCompat.requestPermissions((Activity) mContext, new String[]{permission}, requestCode);
                            }
                        })
                        .setNegativeButton("取消", null)
                        .create().show();
            } else {
                ActivityCompat.requestPermissions((Activity) mContext, new String[]{permission}, requestCode);
            }
        }
    }


}
